<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>期初订单审批</title>
    <script type="text/javascript">
      var openingStockAuditWin;
      var openingStockId;
      Ext.onReady(function(){

        var orderNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '单号',
          name: 'order_number',
          labelWidth: 30,
          anchor: '100%'
        });

        var vendor =  Ext.create('Ext.form.ComboBox', {
          fieldLabel: '供应商',
          store: vendorStore,
          valueField: 'id',
          displayField: 'name',
          name: 'vendor_id',          
          loadingText: '搜索中...',
          //pageSize: 10,
                    
          minChars: 0,        
          editable:false,          
          anchor: '100%'
        });
        
        var toWarehouse =  Ext.create('Ext.form.ComboBox', {
          fieldLabel: '到仓库',
          store: warehouseStore,
          valueField: 'id',
          displayField: 'name',
          name: 'to_warehouse_id',
          loadingText: '搜索中...',
          minChars: 0,
          editable:false,
          anchor: '100%'
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '期初日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });

        var endDate = Ext.create('Ext.form.field.Date', {
          labelWidth: 30,
          fieldLabel: '至',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var submitButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            openingStockAuditDS.removeAll();
            
            openingStockAuditDS.getProxy().extraParams.order_number = orderNumber.getValue();
            openingStockAuditDS.getProxy().extraParams.vendor_id = vendor.getValue();
            openingStockAuditDS.getProxy().extraParams.to_warehouse_id = toWarehouse.getValue();
            openingStockAuditDS.getProxy().extraParams.begin_date = beginDate.getRawValue();
            openingStockAuditDS.getProxy().extraParams.end_date = endDate.getRawValue();
            openingStockAuditDS.getProxy().extraParams.start = 0;
            openingStockAuditDS.load();
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 60,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1150},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .12,
                      layout: 'anchor',
                      xtype: 'container',                      
                      items: [orderNumber]
                    }, {
                      columnWidth: .17,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [toWarehouse]
                    }, {
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [vendor]
                    },{
                      columnWidth: .14,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [beginDate]
                    }, {
                      columnWidth: .13,
                      layout: 'anchor',
                      xtype: 'container',                      
                      items: [endDate]
                    }, {
                      columnWidth: .11,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [submitButton]
                    }, {
                      columnWidth: .11,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //被选择的订单号。
        var openingStockId;

        var openingStockAuditDS =  Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'OpeningStockModel',
          proxy : {
            type : 'ajax',
            url : '/opening_stock/getAllOpeningStocksByAuditStatus',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize:pageSize,
          autoLoad:true
        });

        //生成要订单的产品的Grid
        var openingStockAuditGrid = Ext.create('Ext.grid.Panel', {
          title:'初始库存订单审批列表',
          store: openingStockAuditDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '单号',
              dataIndex: 'order_number',
              width: 120
            },{
              header: '供应商',
              dataIndex: 'vendor_name',
              width: 170
            },{
              header: '总金额',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 95
            },{
              header: '进项税',
              dataIndex: 'value_added_tax',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 85
            },{
              header: '总金额(含税)',
              dataIndex: 'tax_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 100
            },{
              header: '期初日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 80
            },{
              header: '到仓库',
              dataIndex: 'warehouse_name',
              width: 85
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 70
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 230
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 50
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 150
            },{
              header: '处理状态',
              dataIndex: 'deal_with_status',
              width: 70
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: 360,
          frame: true         
        });

        //############ 订单-右键菜单 ##########
        var auditOrder = Ext.create('Ext.menu.Item', {
          text: '【审批期初订单】',
          //iconCls: 'album',
          handler: rightMenuAuditFn
        });
        function rightMenuAuditFn(){
          openingStockAuditWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 305,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '期初订单审批',
            autoLoad: {
              url: "/opening_stock/auditOpeningStock?opening_stock_id="+openingStockId,
              scripts: true
            }
          });
          openingStockAuditWin.on("destroy",function(){
            openingStockAuditDS.load();
          });
          openingStockAuditWin.resizable = false;
          openingStockAuditWin.show();
        }
 
        var rightClick = Ext.create('Ext.menu.Menu', {
          items: [auditOrder]
        });

        openingStockAuditGrid.on('itemcontextmenu', function(grid, record,item,index,e,options){
          openingStockId = openingStockAuditDS.getAt(index).get('id');
          var auditingStatus = openingStockAuditDS.getAt(index).get('auditing_status');
          if (auditingStatus.indexOf("通过") > -1){
            auditOrder.setDisabled(true);
          }else {
            auditOrder.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        
        openingStockAuditDS.load();

        openingStockAuditGrid.on('itemdblclick', function(gridPanel, record,item,index,e,options){
          openingStockId = openingStockAuditDS.getAt(index).get('id');
          var openingStockNumber = openingStockAuditDS.getAt(index).get('order_number');
          //openingStockAuditDS.load();
          openingStockDetailDS.removeAll();
          openingStockDetailDS.load({params:{'opening_stock_id': openingStockId}});
          openingStockDetailGrid.setTitle("期初订单："+openingStockNumber+"，的详细信息");
        });

        //--------------------------期初订单详细，双击后可显示数据----------------
        //期初订单Grid Store Detail
        var openingStockDetailDS =  Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'OpeningStockDetailModel',
          proxy : {
            type : 'ajax',
            url : '/opening_stock/getOpeningStockDetailsById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });

        //生成要订单的产品的Grid
        var openingStockDetailGrid = Ext.create('Ext.grid.Panel', {
          title:'订单详细列表',
          store: openingStockDetailDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 230
            },{
              header: '产品编号',
              dataIndex: 'product_number',
              width: 90
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 80
            },{
              header: '数量',
              align: 'right',
              dataIndex: 'quantity',
              width: 60
            },{
              header: '单价',
              align: 'right',
              dataIndex: 'unit_price',
              renderer: Ext.util.Format.numberRenderer('0,000.0000000000'),
              width: 110
            },{
              header: '总价',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 70
            },{
              header: '进项税',
              dataIndex: 'value_added_tax',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 85
            },{
              header: '单价(含税)',
              dataIndex: 'tax_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 80
            },{
              header: '总价(含税)',
              dataIndex: 'tax_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 85
            },{
              header: '套装?',
              dataIndex: 'is_suit',
              width: 50
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }],
          loadMask: true,
          width: screenWidth-165,
          height: dgHeight,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });


        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: totalHeight,autoScroll:true,
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          /*layout: {
            type:'vbox',
            align:'stretch'
          },*/
          frame:false,          
          layout: {type: 'table',columns:1},          
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[queryFormPanel,openingStockAuditGrid,openingStockDetailGrid]
        });
        workbenchPanel.render("opening_stock_audit_list");
        
      })
    </script>
  </head>
  <body>
    <div id="opening_stock_audit_list"></div>
  </body>
</html>
